package com.suray.wcs.service.init.device;

import com.suray.basic.wcs.plc.pojo.Robot;
import com.suray.basic.wcs.plc.pojo.device.Scanner;
import com.suray.basic.wcs.utils.Coord;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.wcs.service.system.entity.DeviceDB;
import com.suray.wcs.service.system.service.IDevicedbService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 外部设备构造器
 *
 * @author mj
 * @date 2022-6-16
 */
@Component
public class DeviceManager {

    private List<DeviceDB> deviceDBS;

    private static RobotManager robotManager;
    private static ScanManager scanManager;

    public DeviceManager() {
        IDevicedbService devicedbService = SpringContextHolder.getBean(IDevicedbService.class);
        this.deviceDBS = devicedbService.list();

        this.scanManager = new ScanManager();
        this.robotManager = new RobotManager();
    }

    @PostConstruct
    public void createDevice() {
        deviceDBS.forEach(deviceDB -> {
            switch (deviceDB.getType()) {
                case WEIGHT:
                case LED:
                case AGV:
                case REMARK:
                    break;
                case SCANNER:
                    scanManager.createScan(deviceDB);
                    break;
                case MACHINE_HAND:
                    robotManager.createRobot(deviceDB);
                    break;
            }
        });
    }

    public static Robot getByRobotNo(String robotNo) {
        return robotManager.getByRobotNo(robotNo);
    }

    public static Robot getByRobotCoord(Coord coord) {
        return robotManager.getRobotByCoord(coord);
    }

    public static Scanner getByScanCoord(Coord coord) {
        return scanManager.getScanByCoord(coord);
    }

    public static Scanner getByScanNo(String no) {
        return scanManager.getScanByNo(no);
    }

    public static Scanner getByScanName(String name) {
        return scanManager.getScanByName(name);
    }
}
